<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\FreezeAction;
use App\Http\Controllers\Controller;
use App\Models\Base\Charge;
use App\Models\Login\Account;
use App\Service\ChannelService;
use App\Service\RequestService;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 *用户管理.
 */
class AccountController extends Controller
{
    /**
     * [index 用户列表].
     *
     * @Author   王文凡
     * @DateTime 2018-04-09
     *
     * @version  1.0
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.player_related'));
            $content->description(trans('game.player_account'));
            $content->body($this->grid()->render());
            $content->row(view('admin.users.account'));
        });
    }

    protected function grid()
    {
        $grid = Admin::grid(Account::class, function (Grid $grid) {
            $grid->model()->select(DB::raw('*,ac_platform as plat_name'));
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableActions();
            $grid->ac_id(trans('game.account_id'));
            $grid->ac_account(trans('game.player_account'));
            $grid->ac_platform(trans('game.platform_id'));

            $grid->plat_name(trans('game.where_the_platform'))->display(function ($plat) {
                $pluck = Charge::where('channel_no', $plat)->pluck('name');

                return $pluck[0] ?? $plat;
            });

            $grid->channelId(trans('game.channel'))->display(function ($plat) {
                $pluck = Charge::where('channel_no', $plat)->pluck('name');

                return $pluck[0] ?? $plat;
            });

            $grid->ac_createTime(trans('admin.created_at'))->display(function ($create_at) {
                return date('Y-m-d H:i:s', $create_at);
            })->sortable();

            $grid->ac_lastLogintime(trans('game.last_login_date'))->display(function ($last_login_at) {
                return date('Y-m-d H:i:s', $last_login_at);
            });
            $grid->player(trans('game.role_number'))->display(function ($player) {
                if ($term = binary_to_term($player)) {
                    return count($term);
                } else {
                    return 0;
                }
            });

            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('channelId', trans('game.channel'))->select(ChannelService::getChannelNameNo());
                $filter->like('ac_account', trans('game.account'));
            });

            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                    $batch->add(trans('game.sealing_account'), new FreezeAction());
                });
            });
        });

        return $grid;
    }

    /**
     * [banned 禁封玩家账号].
     *
     * @Author   王文凡
     * @DateTime 2018-04-10
     *
     * @version  1.0
     *
     * @param Request $request [description]
     *
     * @return array
     */
    public function banned(Request $request)
    {
        $post = $request->post();
        if (empty($post['exp_time'])) {
            return ['status' => 0, 'message' => trans('game.unsealing_time')];
        }
        $time = strtotime($post['exp_time']) - time();
        $num = 0;
        $admin_name = \Auth::guard('admin')->user()->name;
        $ip = $request->getClientIp();
        $now = Carbon::now()->toDateTimeString();
        foreach ($post['ids'] as $id) {
            $info = Account::find($id);
            $ret = RequestService::banAccount($info->ac_platform, $info->ac_account, 1, $time);
            if (!$ret) {
                ++$num;
                Log::error(trans('game.banned_failure'), $post);
            } else {
                //记录日志
                $exp_time = $post['exp_time'] ? $post['exp_time'] : 0;
                DB::table('log_account_bands')->insert([
                    'account_id' => $id,
                    'account' => $info->ac_account,
                    'platform_id' => $info->ac_platform,
                    'delay_time' => $exp_time,
                    'admin_name' => $admin_name,
                    'client_ip' => $ip,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]);
            }
        }

        return ['status' => 0, 'message' => trans('game.operation_successful').$num.trans('game.a_failure')];
    }
}
